<template>
  <el-dialog
      :title="!form.id ? '新增' : '修改'"
      :close-on-click-modal="false"
      fullscreen
      @close="closeDialog()"
      :visible.sync="visible">
    <el-form :model="form" ref="form" label-width="160px" class="demo-ruleForm">
      <el-form-item required label="项目编号" prop="projectCode">
        <el-select v-model="form.projectCode" :disabled="form.id != 0" @change="changeProjectCode" placeholder="请选择">
          <el-option
              v-for="item in projectCodeOptions"
              :key="item.prProjectNumber"
              :label="item.prProjectName"
              :value="item.prProjectNumber">
          </el-option>
        </el-select>
      </el-form-item>
      <el-row v-for="(item, index) in form.sampleDetailsList" :key="index">
        <el-form-item label="主机" style="float: left">
          {{ index + 1 }}
        </el-form-item>
        <el-form-item label="样机商标·型号" style="width: 300px;float: left">
          <el-input v-model="form.prototypeTrademark"></el-input>
        </el-form-item>
        <!--        <el-col style="">-->
        <el-form-item prop="projectName" label="样品编号" style="width: 300px;float: left">
          <el-input type="text" placeholder="请输入样品编号" v-model="item.sampleNumber"/>
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col style="">-->
        <el-form-item prop="unit" label="出厂编号" style="width: 300px;float: left">
          <el-input type="text" placeholder="请输入出厂编号" v-model="item.appearanceNumber"/>
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col>-->
        <el-form-item prop="designValue" label="出厂日期" style="width: 230px;float: left">
          <el-date-picker v-model="item.appearanceDate" style="width: 180px" type="date" value-format="yyyy-MM-dd"
                          placeholder="选择出厂日期"></el-date-picker>
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col>-->
        <el-form-item prop="designValue" label="用途" style="width: 300px;float: left">
          <el-input type="text" placeholder="请输入用途" v-model="item.purpose"/>
        </el-form-item>
        <!--        </el-col>-->
      </el-row>
      <el-row v-if="product.isCover === '1'">
        <el-form-item style="margin-left: -160px" v-for="(item, index) in form.covers" :key="index">
          <el-form-item label="涵盖机" style="float: left">
            {{ index + 1 }}
          </el-form-item>
          <el-form-item label="样机商标·型号" style="width: 300px;float: left">
            <el-input v-model="form.covermark"></el-input>
          </el-form-item>
          <!--        <el-col style="">-->
          <el-form-item prop="projectName" label="样品编号" style="width: 300px;float: left">
            <el-input type="text" placeholder="请输入样品编号" v-model="item.sampleNumber"/>
          </el-form-item>
          <!--        </el-col>-->
          <!--        <el-col style="">-->
          <el-form-item prop="unit" label="出厂编号" style="width: 300px;float: left">
            <el-input type="text" placeholder="请输入出厂编号" v-model="item.appearanceNumber"/>
          </el-form-item>
          <!--        </el-col>-->
          <!--        <el-col>-->
          <el-form-item prop="designValue" label="出厂日期" style="width: 230px;float: left">
            <el-date-picker v-model="item.appearanceDate" style="width: 180px" type="date" value-format="yyyy-MM-dd"
                            placeholder="选择出厂日期"></el-date-picker>
          </el-form-item>
          <!--        </el-col>-->
          <!--        <el-col>-->
          <el-form-item prop="designValue" label="用途" style="width: 300px;float: left">
            <el-input type="text" placeholder="请输入用途" v-model="item.purpose"/>
          </el-form-item>
        </el-form-item>
        <!--        </el-col>-->
      </el-row>
      <el-form-item label="样品等级">
        <el-input v-model="form.sampleGrade" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="抽样基数">
        <el-input v-model="form.sampleNumber" type="number" style="width: 1450px"></el-input>
        <template>
          <el-radio v-model="type" label="台">台</el-radio>
          <el-radio v-model="type" label="套">套</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="抽样数量">
        <el-input v-model="form.samplingBasicNumber" style="width: 1450px" type="number"></el-input>
        <template>
          <el-radio v-model="type1" label="台">台</el-radio>
          <el-radio v-model="type1" label="套">套</el-radio>
        </template>
      </el-form-item>
      <el-form-item label="封签类型">
        <el-checkbox-group v-model="form.sampleTypeList">
          <el-checkbox label="纸封签"></el-checkbox>
          <el-checkbox label="铅封"></el-checkbox>
          <el-checkbox label="印记封"></el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="封签部位">
        <el-input v-model="form.sealingPosition" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="抽样方法">
        <el-input v-model="form.sealMethod" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="样品状态">
        <el-input v-model="form.sampleStatus" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="抽样地点">
        <el-input v-model="form.sampleSite" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="样品存放地点">
        <el-input v-model="form.sampleLocation" style="width: 1450px"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="24">
            <span style="margin-left: 30px; font-weight: bold; font-size: 20px;">
              单位
            </span>
        </el-col>
      </el-row>
      <el-row v-for="(item, index) in form.sampleSheetWorkplaceDetailList" :key="index">
        <!--        <el-col :span="8">-->
        <el-form-item prop="column" label="栏目" style="width: 300px;float: left">
          {{ item.columnName }}
          <!--            <el-input type="text" placeholder="请输入栏目" v-model="item.columnName"/>-->
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col :span="8">-->
        <el-form-item prop="unit" label="单位名称" style="width: 400px;float: left">
          <el-input type="text" placeholder="请输入单位名称" v-model="item.unitName"/>
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col :span="8">-->
        <el-form-item prop="designValue" label="邮政编码" style="width: 400px;float: left">
          <el-input type="text" placeholder="请输入邮政编码" v-model="item.postalCode"/>
        </el-form-item>
        <!--        </el-col>-->
        <!--        <el-col :span="8">-->
        <el-form-item prop="phone" label="电话" style="width: 400px;float: left">
          <el-input type="text" placeholder="请输入电话" v-model="item.phone"/>
        </el-form-item>
        <!--        </el-col>-->
      </el-row>
      <el-form-item label="检验依据">
        <el-input v-model="form.inspectionBasis" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.remark" style="width: 1450px"></el-input>
      </el-form-item>
      <el-form-item label="抽样日期">
        <el-date-picker v-model="form.sampleDate" type="date" value-format="yyyy年MM月dd日"
                        placeholder="选择抽样日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      visible: false,
      type:'台',
      type1:'台',
      form: {
        id: 0,
        projectCode: null,
        sampleGrade: '合格品',
        sealMethod: '随机抽样',
        sealingPosition: '样品本体',
        sampleSite: '',
        covermark: '',
        sampleNumber: '',
        inspectionBasis: '',
        samplingBasicNumber: '',
        sampleDetailsList: [],
        covers: [],
        sampleSheetWorkplaceDetailList: [
          {columnName: "样机生成单位",unitName: "镇原县兴龙农业机械有限公司", postalCode: "730046", phone: "0931-8320060"},
          {columnName: "供样单位",unitName: "镇原县兴龙农业机械有限公司", postalCode: "730046", phone: "0931-8320060"},
          {columnName: "抽样单位", unitName: "镇原县兴龙农业机械有限公司", postalCode: "730046", phone: "0931-8320060"}
        ],
        sampleTypeList: [],
      },
      product: {
        availInspReportPath: "",
        isCover: "",
        manufacturerId: "",
        prCreated: "",
        prId: 1,
        prProducerModel: "",
        prProjectName: "",
        prProjectNumber: "",
        prProjectType: "",
        producerId: "",
        productExecutionText: "",
        productInnoDescReportPath: "",
        production: 0,
        productionLicence: "",
        salesVolume: 0,
        sellingArea: "",
        updatedAt: "",
      },
      projectCodeOptions: [],
      projectCodeMap: {},
    }
  },
  methods: {
    changeProjectCode(val) {
      let num
      let num1
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/authenticationtask/getSampleSheet/${val}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        this.form.sampleNumber = data.authenticationTask.samplingBase;
        this.form.samplingBasicNumber = data.authenticationTask.samplingAmount;
        this.form.inspectionBasis = `DG-T ` + data.authenticationTask.identificationBasis + data.authenticationTask.product;
        num = data.authenticationTask.samplingAmount + 1;
        for (let i = 1; i < num; ++i) {
          this.form.sampleDetailsList.push({})
          this.form.sampleDetailsList[i - 1].sampleNumber = `${val}-${i}`;
        }
        this.form.sampleDetailsList.for
      })
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getProductInfo/${val}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        this.product = {
          ...data.productInfo
        };
        this.form.sampleSite = data.productInfo.manufacturerId;
        this.form.prototypeTrademark = data.productInfo.prProducerModel + data.productInfo.prProjectName;
      });
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/product/getCover/${val}`),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        num1 = data.product.covers.length + 1;
        for (let i = 1; i < num1; ++i) {
          this.form.covers.push({})
          console.log(this.form.covers[i])
          this.form.covermark = data.product.prProducerModel + data.product.prProjectName;
          this.form.covers[i - 1].sampleNumber = data.product.covers[i - 1].name;
          this.form.covers[i - 1].sampleSupplied = data.product.covers[i - 1].value;
        }
        this.form.covers.for
      })
    },
    getProductCode() {
      this.$http({
        url: this.$http.adornUrl('/reportapproval/product/getProjectNumber'),
        method: 'get',
        data: this.$http.adornParams({})
      }).then((data) => {
        if (data && data.code === 0) {
          this.projectCodeOptions = data.productList;
          this.projectCodeOptions.forEach(item => this.projectCodeMap[item.prProjectNumber] = item);
          // this.form.sampleSite =
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    init(id) {
      this.getProductCode();
      this.form.id = id || 0
      this.visible = true
      console.log("aaaaa", this.sampleDetailsList)
      this.sampleDetailsList = []
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (this.form.id) {
          this.$http({
            url: this.$http.adornUrl(`/sampleSheet/${id}`),
            method: 'get',
          }).then(res => {
            if (res.code === 0) {
              this.form = res.data;
              this.form.sampleTypeList = res.data.sampleType.split(',');
            }
          })
        }
      })
    },
    resetForm() {
      this.form = {
        id: 0,
        projectCode: null,
        sampleDetailsList: [],
        sampleSheetWorkplaceDetailList: [
          {columnName: "样机生成单位"},
          {columnName: "供样单位"},
          {columnName: "抽样单位"}
        ],
        sampleTypeList: []
      },
          this.projectCodeOptions = {}
      this.projectCodeMap = {}
    },
    closeDialog() {
      this.resetForm();
      this.$emit('refreshDataList');
    },
    // 表单提交
    submit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.sampleType = this.form.sampleTypeList.join(',');
          this.$http({
            url: this.$http.adornUrl(`/sampleSheet`),
            method: this.form.id ? 'put' : 'post',
            data: this.$http.adornData({
              "projectCode": this.form.projectCode,
              "prototypeTrademark": this.form.prototypeTrademark,
              "sampleGrade": this.form.sampleGrade,
              "sampleNumber": this.form.sampleNumber + this.type,
              "samplingBasicNumber": this.form.samplingBasicNumber + this.type,
              "sampleType": this.form.sampleType,
              "sealingPosition": this.form.sealingPosition,
              "sealMethod": this.form.sealMethod,
              "sampleStatus": this.form.sampleStatus,
              "sampleSite": this.form.sampleSite,
              "sampleLocation": this.form.sampleLocation,
              "inspectionBasis": this.form.inspectionBasis,
              "remark": this.form.remark,
              "sampleDate": this.form.sampleDate,
              "sampleDetailsList": this.form.sampleDetailsList,
              "coversList": this.form.covers,
              "sampleSheetWorkplaceDetailList": this.form.sampleSheetWorkplaceDetailList
            })
          }).then((data) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>